/***
    Amrita ITEWS - Copyright (C) 2009 Amrita Vishwa Vidyapeetham, Amritapuri.
                                       (http://www.amrita.edu)
    ***************************************************************************
    This file is part of the Amrita ITEWS distribution.
    Amrita ITEWS is free software; you can redistribute it and/or modify it 
    under the terms of the GNU General Public License (version 2) as published
    by the Free Software Foundation AND MODIFIED BY the Amrita ITEWS exception.
    ***NOTE*** The exception to the GPL is included to allow you to distribute
    a combined work that includes Amrita ITEWS without being obliged to provide
    the source code for proprietary components outside of the Amrita ITEWS
    software. Amrita ITEWS is distributed in the hope that it will be useful, 
    but WITHOUT ANY WARRANTY; without even the implied warranty of 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
    Public License for more details. You should have received a copy of the GNU
    General Public License and the Amrita ITEWS license exception along with 
    Amrita ITEWS if not then it can be viewed here: 
    http://itews.amrita.ac.in/license.html.

    Documentation, latest information, license and contact details are at:
    http://itews.amrita.ac.in/

    Amrita ITEWS source code can be found at:
    http://code.google.com/p/itews

    The initial code-base of Amrita ITEWS was developed by Amrita Vishwa 
    Vidyapeetham as part of the project titled,"Intelligent & Interactive 
    Telematics using Emerging Wireless Technologies for Transport Systems 
    (ITEWS)" sponsored by Technology Information Forecasting and Assessment 
    Council (TIFAC), India.
***/

package gpsservice;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

public final class GPSConfig
{
	public static final String GVIA_COMMON_CONFIG_FILE = "/etc/gvia/gviaConfig.properties"; 
	public static final String GPS_CONFIG_FILE = "gpsserviceConfig.properties";
	private String serialPortName;
	private int serialBaud;
	private int gpsPollTimeInterval;
	private String user;
	private String password;
	private static GPSConfig singleton = null;
	private Properties gviaCommonProps;
	private Properties configProperties;
	private String jdbcConnectionURI;
	private int validFixQuality;
	
	private GPSConfig() throws Exception
	{
		InputStream is = null;
		InputStream gviaIs = null;
		try
		{
			configProperties = new Properties();
			is = this.getClass().getResourceAsStream(GPS_CONFIG_FILE);
			if (is == null)
				throw new Exception(GPS_CONFIG_FILE + " file not found");
			configProperties.load(is);
			is.close();
			gviaCommonProps = new Properties();
			gviaIs = new FileInputStream(GVIA_COMMON_CONFIG_FILE);
			gviaCommonProps.load(gviaIs);
			gviaIs.close();
			setPassword();
			setUser();
			setSerialPortName();
			setSerialBaud();
			setValidFixQuality();
			setGpsPollTimeInterval();
			setJdbcConnectionURI();
		}
		catch(Exception e)
		{
			if (is != null)
				is.close();
			if (gviaIs != null)
				gviaIs.close();
			throw e;
		}
	}

	public static GPSConfig getConfig() throws Exception 
	{
		try
		{
			if (singleton == null)
			{
				singleton = new GPSConfig();
			}
		}
		catch(Exception e)
		{
			throw e;
		}
		return singleton;
	}
	
	public String getPassword() 
	{
		return password;
	}

	public void setPassword() throws Exception
	{
		String propValue = gviaCommonProps.getProperty("password");
		if (propValue == null)
			throw new Exception("password not added in " + GVIA_COMMON_CONFIG_FILE + " file");
		else
			password = propValue;
	}

	public String getUser() 
	{
		return user;
	}

	public void setUser() throws Exception
	{
		String propValue = gviaCommonProps.getProperty("user");
		if (propValue == null)
			throw new Exception("user not added in " + GVIA_COMMON_CONFIG_FILE + " file");
		else
			user = propValue;
	}

	private void setGpsPollTimeInterval() throws Exception
	{
		String propValue = gviaCommonProps.getProperty("gpsPollTimeInterval");
		if (propValue == null)
			throw new Exception("gpsPollTimeInterval not added in " + GVIA_COMMON_CONFIG_FILE + " file");
		else
			gpsPollTimeInterval = Integer.parseInt(propValue);
	}
	
	public int getGpsPollTimeInterval()
	{
		return gpsPollTimeInterval;
	}
	
	private void setSerialPortName() throws Exception
	{
		String propValue = gviaCommonProps.getProperty("serialPort");
		if (propValue == null)
			throw new Exception("serialPort not added in " + GVIA_COMMON_CONFIG_FILE + " file");
		else
			serialPortName = propValue;
	}
	
	public String getSerialPortName()
	{
		return serialPortName;
	}
	
	private void setSerialBaud() throws Exception
	{
		String propValue = gviaCommonProps.getProperty("serialBaud");
		if (propValue == null)
			throw new Exception("serialBaud not added in " + GVIA_COMMON_CONFIG_FILE + " file");
		else
			serialBaud = Integer.parseInt(propValue);
	}
	
	public int getSerialBaud()
	{
		return serialBaud;
	}
	
	private void setValidFixQuality() throws Exception
	{
		String propValue = gviaCommonProps.getProperty("validFixQuality");
		if (propValue == null)
			throw new Exception("validFixQuality not added in " + GVIA_COMMON_CONFIG_FILE + " file");
		else
			validFixQuality = Integer.parseInt(propValue);
	}
	
	public int getValidFixQuality()
	{
		return validFixQuality;
	}
	
	private void setJdbcConnectionURI() throws Exception
	{
		String propValue = configProperties.getProperty("jdbcConnectionURI");
		if (propValue == null)
			throw new Exception("jdbcConnectionURI not added in " + GPS_CONFIG_FILE + " file");
		else
			jdbcConnectionURI = propValue;
	}
	
	public String getJdbcConnectionURI()
	{
		return jdbcConnectionURI;
	}

}
